/**
 * Copyright 2015 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2015年11月17日 上午10:59:09
 */
package com.absir.shoumeng.asset;

import com.absir.appserv.system.asset.AssetServer;
import com.absir.appserv.system.bean.JPayTrade;
import com.absir.appserv.system.helper.HelperEncrypt;
import com.absir.appserv.system.service.BeanService;
import com.absir.appserv.system.service.utils.PayUtils;
import com.absir.bean.basis.Base;
import com.absir.core.kernel.KernelObject;
import com.absir.server.in.Input;
import com.absir.server.value.Binder;
import com.absir.server.value.Body;
import com.absir.server.value.Server;
import com.absir.shoumeng.service.ShouMengService;

/**
 * @author absir
 *
 */
@Base
@Server
public class asset_shoumeng extends AssetServer {

	/**
	 * @author absir
	 *
	 */
	public static class PayResult {
		public String orderId;// 渠道商订单 ID String 必填 是
		public String uid;// 渠道商用户唯一标识 String 必填 是
		public int amount;// 充值金额(单位：元) int 必填 是
		public String coOrderId;// 游戏商订单号 String 必填 是
		public int serverId;// 充值服的 ID int 必填 否
		public int success;// 订单状态：0 成功，1 失败 int 必填 是
		public String msg;// 订单失败时的错误信息 String 否 否
		public String sign;// MD5 签名结果 String 必填 否
	}

	/**
	 * @param result
	 * @param input
	 * @return
	 */
	@Body
	public String notify(@Binder PayResult result, Input input) {
		if (result.success == 0) {
			String orderId = result.coOrderId;
			JPayTrade payTrade = BeanService.ME.get(JPayTrade.class, orderId);
			if (payTrade != null && payTrade.getAmount() <= result.amount) {
				String sign = "orderId=" + result.orderId + "&uid=" + result.uid + "&amount=" + result.amount + "&coOrderId="
						+ result.coOrderId + "&success=" + result.success + "&secret=" + ShouMengService.CONFIG.getMerchantKey();
				sign = HelperEncrypt.encryptionMD5(sign).toLowerCase();
				if (KernelObject.equals(result.sign, sign)) {
					payTrade.setPlatform(ShouMengService.PLATFORM_NAME);
					PayUtils.proccessDone(payTrade);

				} else {
					return "FAIL";
				}
			}
		}

		return "SUCCESS";
	}
}
